package com.lin.lazybone;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lin.lazybone.application.BaseActivity;
import com.lin.lazybone.entity._User;
import com.lin.lazybone.fragment.HomeFragment;
import com.lin.lazybone.fragment.MesFragment;
import com.lin.lazybone.fragment.OrderFragment;
import com.lin.lazybone.fragment.TestFragment;
import com.lin.lazybone.ui.LoginActivity;
import com.lin.lazybone.ui.PerDataActicity;
import com.lin.lazybone.utils.L;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.newim.BmobIM;
import cn.bmob.newim.core.ConnectionStatus;
import cn.bmob.newim.listener.ConnectListener;
import cn.bmob.newim.listener.ConnectStatusChangeListener;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import de.hdodenhof.circleimageview.CircleImageView;

public class MainActivity extends BaseActivity implements View.OnClickListener, NavigationView.OnNavigationItemSelectedListener
{
    private TabLayout tablayout;
    private ViewPager viewPager;
    //标题栏头像
    private CircleImageView tool_touxiang;
    //toolbar中的居中标题
    TextView mtl_title;
    //整体布局，带有滑动菜单
    DrawerLayout drawer_layout;
    //标题栏扫一扫按钮
//    ImageView toolbar_scan;
    //侧滑菜单布局
    NavigationView nav_view;
    //侧滑菜单用户名字
    private TextView nav_username;
    //侧滑菜单用户简介
    private TextView nav_qianming;
    //退出用户按钮
    Button nav_logout_bt;


    private List<Fragment> vpFragmentList;
    private List<String> mTitles;
    private int[] mImgs = new int[]{R.drawable.home, R.drawable.find, R.drawable.order};
    private int[] mImgs2 = new int[]{R.drawable.home2, R.drawable.find2, R.drawable.order2};


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //使状态栏透明化
        if (Build.VERSION.SDK_INT >= 21)
        {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_main);

        initData();
        initView();

    }

    //初始化界面
    private void initView()
    {
        //初始化各控件
        mtl_title = (TextView) findViewById(R.id.mtl_title);
        tablayout = (TabLayout) findViewById(R.id.tablayout);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        tool_touxiang = (CircleImageView) findViewById(R.id.tool_touxiang);
        tool_touxiang.setOnClickListener(this);
        drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout);
//        toolbar_scan = (ImageView) findViewById(R.id.toolbar_scan);
//        toolbar_scan.setOnClickListener(this);
        nav_view = (NavigationView) findViewById(R.id.nav_view);
        nav_view.setNavigationItemSelectedListener(this);
        nav_logout_bt = (Button) findViewById(R.id.nav_logout_bt);
        nav_logout_bt.setOnClickListener(this);
        //获取侧滑栏头部
        View headerView = nav_view.getHeaderView(0);
        nav_username = (TextView) headerView.findViewById(R.id.nav_username);
        nav_qianming = (TextView) headerView.findViewById(R.id.nav_qianming);

        //设置最新用户信息
        _User user = BmobUser.getCurrentUser(_User.class);
        nav_qianming.setText(user.getUser_signature());
        nav_username.setText(user.getUser_nickname());

        //初始化设置标题为主页
        mtl_title.setText("主页");

        //预加载
        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager())
        {
            @Override
            public Fragment getItem(int position)
            {
                return vpFragmentList.get(position);
            }

            @Override
            public int getCount()
            {
                return vpFragmentList.size();
            }
        });

        //初始化TabLayout
        tablayout.setupWithViewPager(viewPager);
        //隐藏下划线
        tablayout.setSelectedTabIndicatorHeight(0);
        for (int i = 0; i < 3; i++)
        {
            //获得到对应位置的Tab
            TabLayout.Tab itemTab = tablayout.getTabAt(i);
            if (itemTab != null)
            {
                //刚开始第一个选项卡图标显示被选中
                itemTab.setCustomView(R.layout.tab_item);
                if (i == 0)
                {
                    TextView textView = (TextView) itemTab.getCustomView().findViewById(R.id.tv_name);
                    textView.setText(mTitles.get(i));
                    textView.setTextColor(Color.BLUE);
                    ImageView imageView = (ImageView) itemTab.getCustomView().findViewById(R.id.iv_img);
                    imageView.setImageResource(mImgs2[i]);
                } else
                {
                    TextView textView = (TextView) itemTab.getCustomView().findViewById(R.id.tv_name);
                    textView.setText(mTitles.get(i));
                    ImageView imageView = (ImageView) itemTab.getCustomView().findViewById(R.id.iv_img);
                    imageView.setImageResource(mImgs[i]);
                }
            }
        }
        //给Viewpager设置监听，更改Tablayout图标的改变
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            private int prePosition = 0;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
            {

            }

            @Override
            public void onPageSelected(int position)
            {
                mtl_title.setText(mTitles.get(position));
                //被选中的选项卡改成绿色，没被选中的时候显示为黑色
                TabLayout.Tab itemTab = tablayout.getTabAt(prePosition);
                TextView textView = (TextView) itemTab.getCustomView().findViewById(R.id.tv_name);
                textView.setTextColor(Color.BLACK);
                ImageView imageView = (ImageView) itemTab.getCustomView().findViewById(R.id.iv_img);
                imageView.setImageResource(mImgs[prePosition]);
                itemTab = tablayout.getTabAt(position);
                textView = (TextView) itemTab.getCustomView().findViewById(R.id.tv_name);
                textView.setTextColor(Color.BLUE);
                imageView = (ImageView) itemTab.getCustomView().findViewById(R.id.iv_img);
                imageView.setImageResource(mImgs2[position]);
                prePosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state)
            {

            }
        });

    }

    //初始化数据
    private void initData()
    {
        //viewpage的fragment
        vpFragmentList = new ArrayList<>();
        //添加主页面
        vpFragmentList.add(new HomeFragment());
        //添加发现页面
        vpFragmentList.add(new TestFragment());
        //添加订单页面
        vpFragmentList.add(new OrderFragment());

        //标题
        mTitles = new ArrayList<>();
        mTitles.add("主页");
        mTitles.add("消息");
        mTitles.add("订单");
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            //标题栏头像点击事件
            case R.id.tool_touxiang:
                drawer_layout.openDrawer(GravityCompat.START);
                break;
//            //标题栏扫一扫点击事件
//            case R.id.toolbar_scan:
//                //点击扫一扫按钮址的跳转逻辑
//                Toast.makeText(this, "点击了扫一扫", Toast.LENGTH_SHORT).show();
//                break;
            case R.id.nav_logout_bt:
                BmobUser.logOut();
                Toast.makeText(this, "成功退出", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, LoginActivity.class));
                finish();
                break;
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item)
    {
        drawer_layout.closeDrawers();
        switch (item.getItemId())
        {
            //点击了我的资料
            case R.id.nav_mydate:
                //跳转到我的资料界面
                startActivity(new Intent(this, PerDataActicity.class));
                break;
            //点击了设置
            case R.id.nav_setting:
                //跳转到设置界面
                Toast.makeText(this, "跳转到设置界面", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }

}
